﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP;

namespace YArchitech.Plumbing
{
	public partial class FormLayFireHydrant : System.Windows.Forms.Form
	{
		public FormLayFireHydrant(Autodesk.Revit.DB.Document doc)
		{
			this.listInfos = this.sfdr.GetAllNameAndImage("Hydrant");
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
			this.initParameterSetting();
			this.lbfname.Text = this.name;
			if (this.listInfos.Count > 0)
			{
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.GetParasFromDB(listItem);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
				this.lbSymbol.SelectedItem = this.familySymbol;
			}
			this.toolTip1.SetToolTip(this.btnCalc, "计算保护半径");
			this.toolTip1.SetToolTip(this.btnProjectCalc, "计算保护半径");
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.tabProject.SelectedIndex == 0)
			{
				bool waterTankParamFromForm = this.GetWaterTankParamFromForm();
				if (this.fireHydrantParam != null && waterTankParamFromForm)
				{
					this.UserSettingWrite();
					base.DialogResult = DialogResult.OK;
				}
			}
			else
			{
				if (this.tvwProject.SelectedNode.Level == 0)
				{
					YJKMessageBox.Show("您未选择消火栓族或族类型");
					return;
				}
				bool projectWaterTankParamFromForm = this.GetProjectWaterTankParamFromForm();
				if (this.fireHydrantParam != null && projectWaterTankParamFromForm)
				{
					this.UserSettingWrite();
					base.DialogResult = DialogResult.OK;
				}
			}
			this.LayType = 1;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			FormChooseHydrant formChooseHydrant = new FormChooseHydrant(this.name, this.listInfos);
			formChooseHydrant.Owner = this;
			if (formChooseHydrant.ShowDialog() == DialogResult.OK && this.name != formChooseHydrant.ImageName && formChooseHydrant.ImageName != "")
			{
				this.name = formChooseHydrant.ImageName;
				this.lbfname.Text = this.name;
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.GetParasFromDB(listItem);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void GetParasFromDB(ListItem item)
		{
			List<ListItem> infoById = this.sfdr.GetInfoById("Hydrant", int.Parse(item.Tag.ToString()));
			if (infoById.Count > 0)
			{
				foreach (ListItem listItem in infoById)
				{
					if (listItem.Name == "Image")
					{
						if (File.Exists(listItem.Value))
						{
							this.pictureBox1.Image = new Bitmap(listItem.Value);
						}
						else
						{
							this.pictureBox1.Image = null;
						}
					}
					else if (listItem.Name == "FamilySymbol")
					{
						this.UptadaFamilySybol(listItem.Value);
					}
					else if (listItem.Name == "RadiuDiameter")
					{
						this.tbradiu.Text = listItem.Value;
					}
				}
			}
		}

		private void UptadaFamilySybol(string szData)
		{
			this.lbSymbol.Items.Clear();
			foreach (string text in szData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					this.lbSymbol.Items.Add(text);
				}
			}
			if (this.lbSymbol.Items != null && this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedIndex = 0;
			}
		}

		private void initParameterSetting()
		{
			this.InitProjectParameter();
			UserSetting userSetting = new UserSetting();
			this.name = userSetting.GetValue("hydrants", "familyName");
			this.familySymbol = userSetting.GetValue("hydrants", "familySymbol");
			this.tblevel.Text = userSetting.GetValue("hydrants", "offset");
			if (userSetting.GetValue("hydrants", "range") != "")
			{
				this.checkBox.Checked = Convert.ToBoolean(userSetting.GetValue("hydrants", "range"));
				return;
			}
			this.checkBox.Checked = true;
		}

		private void UserSettingWrite()
		{
			this.tblevel.Text = StringTools.RemoveZero(this.tblevel.Text.Trim());
			try
			{
				this.UserSetWrite();
				UserSetting userSetting = new UserSetting();
				userSetting.WriteValue("hydrants", "familyName", this.name);
				userSetting.WriteValue("hydrants", "familySymbol", this.lbSymbol.SelectedItem.ToString());
				userSetting.WriteValue("hydrants", "offset", this.tblevel.Text);
				userSetting.WriteValue("hydrants", "range", this.checkBox.Checked.ToString());
				this.SaveParasFofDB();
			}
			catch (Exception)
			{
			}
		}

		public bool GetWaterTankParamFromForm()
		{
			if (ControlJudgement.IsAllControlEmpty(new List<System.Windows.Forms.Control>
			{
				this.tbradiu,
				this.tblevel
			}))
			{
				return false;
			}
			this.fireHydrantParam = new FireHydrantParam();
			if (double.Parse(this.tbradiu.Text.Trim()) > 0.0)
			{
				this.fireHydrantParam.Radius = double.Parse(this.tbradiu.Text.Trim());
				this.fireHydrantParam.OffSet = double.Parse(this.tblevel.Text.Trim());
				this.fireHydrantParam.OffSet = 1.1;
				this.fireHydrantParam.Range = this.checkBox.Checked;
				this.fireHydrantParam.SymbolName = this.lbSymbol.Text.Trim();
				this.fireHydrantParam.Id = this.id;
				return true;
			}
			YJKMessageBox.Show("保护半径必须为正数");
			this.tbradiu.Focus();
			return false;
		}

		private void SaveParasFofDB()
		{
			List<ListItem> list = new List<ListItem>();
			list.Add(new ListItem("RadiuDiameter", this.fireHydrantParam.Radius.ToString()));
			if (!this.sfdr.SetInfoById("Hydrant", this.id, list))
			{
				YJKMessageBox.Show("保存数据失败");
			}
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (this.tabProject.SelectedIndex == 0)
			{
				bool waterTankParamFromForm = this.GetWaterTankParamFromForm();
				if (this.fireHydrantParam != null && waterTankParamFromForm)
				{
					this.UserSettingWrite();
					base.DialogResult = DialogResult.OK;
				}
			}
			else
			{
				if (this.tvwProject.SelectedNode.Level == 0)
				{
					YJKMessageBox.Show("您未选择消火栓族或族类型");
					return;
				}
				bool projectWaterTankParamFromForm = this.GetProjectWaterTankParamFromForm();
				if (this.fireHydrantParam != null && projectWaterTankParamFromForm)
				{
					this.UserSettingWrite();
					base.DialogResult = DialogResult.OK;
				}
			}
			this.LayType = 2;
		}

		private void btnCalc_Click(object sender, EventArgs e)
		{
			FormProtectRadiusCalc formProtectRadiusCalc = new FormProtectRadiusCalc();
			formProtectRadiusCalc.ShowDialog();
			if (formProtectRadiusCalc.DialogResult == DialogResult.OK)
			{
				this.tbradiu.Text = formProtectRadiusCalc.dRadius.ToString("0.00");
			}
		}

		private void label1_Click(object sender, EventArgs e)
		{
		}

		private void btnProjectCalc_Click(object sender, EventArgs e)
		{
			FormProtectRadiusCalc formProtectRadiusCalc = new FormProtectRadiusCalc();
			formProtectRadiusCalc.ShowDialog();
			if (formProtectRadiusCalc.DialogResult == DialogResult.OK)
			{
				this.hyTxtRadiu.Text = formProtectRadiusCalc.dRadius.ToString("0.00");
			}
		}

		private void InitProjectParameter()
		{
			UserSetting userSetting = new UserSetting();
			this.hyTxtRadiu.Text = (string.IsNullOrEmpty(userSetting.GetValue("hydrants", "hyTxtRadiu")) ? "25" : userSetting.GetValue("hydrants", "hyTxtRadiu"));
			if (userSetting.GetValue("hydrants", "tabSelectedIndex") != "")
			{
				this.tabProject.SelectedIndex = Convert.ToInt32(userSetting.GetValue("hydrants", "tabSelectedIndex"));
			}
			else
			{
				this.tabProject.SelectedIndex = 0;
			}
			if (userSetting.GetValue("hydrants", "projectRange") != "")
			{
				this.chkScope.Checked = Convert.ToBoolean(userSetting.GetValue("hydrants", "projectRange"));
			}
			else
			{
				this.chkScope.Checked = true;
			}
			this.tvwProject.Nodes.Add("机械设备");
			HyProjectFamily.BingTvwFamilys(HyProjectFamily.GetFamilys(BuiltInCategory.OST_MechanicalEquipment, this.doc).FindAll((Family p) => p.Name.Contains("消火栓") || p.Name.Contains("消防软管卷盘箱") || p.Name.Contains("组合式消防柜")).ToList<Family>(), this.tvwProject);
		}

		private void UserSetWrite()
		{
			this.tblevel.Text = StringTools.RemoveZero(this.tblevel.Text.Trim());
			try
			{
				UserSetting userSetting = new UserSetting();
				userSetting.WriteValue("hydrants", "tabSelectedIndex", this.tabProject.SelectedIndex.ToString());
				userSetting.WriteValue("hydrants", "hyTxtRadiu", this.hyTxtRadiu.Text);
				userSetting.WriteValue("hydrants", "projectoffset", this.tblevel.Text);
				userSetting.WriteValue("hydrants", "projectRange", this.chkScope.Checked.ToString());
			}
			catch (Exception)
			{
			}
		}

		private void tvwProject_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvwProject.SelectedNode == null)
			{
				return;
			}
			this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.White;
			if (this.tvwProject.SelectedNode.Level == 0)
			{
				this.projectPreviewPic.Image = null;
				return;
			}
			Family family = null;
			if (this.tvwProject.SelectedNode.Level == 1)
			{
				family = (this.tvwProject.SelectedNode.Tag as Family);
			}
			else if (this.tvwProject.SelectedNode.Level == 2)
			{
				family = (this.tvwProject.SelectedNode.Parent.Tag as Family);
			}
			if (family == null)
			{
				YJKMessageBox.Show("您未选择消火栓族或族类型");
				return;
			}
			string imageByFamily = HyProjectFamily.GetImageByFamily(family, "Hydrant", this.doc);
			if (File.Exists(imageByFamily))
			{
				this.projectPreviewPic.Image = Image.FromFile(imageByFamily);
				return;
			}
			this.projectPreviewPic.Image = null;
		}

		private void tvwProject_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwProject.SelectedNode != null)
			{
				this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.White;
				this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		public bool GetProjectWaterTankParamFromForm()
		{
			if (ControlJudgement.IsAllControlEmpty(new List<System.Windows.Forms.Control>
			{
				this.hyTxtRadiu,
				this.tblevel
			}))
			{
				return false;
			}
			this.fireHydrantParam = new FireHydrantParam();
			if (double.Parse(this.hyTxtRadiu.Text.Trim()) > 0.0)
			{
				this.fireHydrantParam.Radius = double.Parse(this.hyTxtRadiu.Text.Trim());
				this.fireHydrantParam.OffSet = double.Parse(this.tblevel.Text.Trim());
				this.fireHydrantParam.OffSet = 1.1;
				this.fireHydrantParam.Range = this.chkScope.Checked;
				this.fireHydrantParam.SymbolName = this.tvwProject.SelectedNode.Name;
				if (this.tvwProject.SelectedNode.Level == 1)
				{
					this.fs = (this.tvwProject.SelectedNode.Nodes[0].Tag as FamilySymbol);
				}
				else if (this.tvwProject.SelectedNode.Level == 2)
				{
					this.fs = (this.tvwProject.SelectedNode.Tag as FamilySymbol);
				}
				this.fireHydrantParam.Id = this.id;
				return true;
			}
			YJKMessageBox.Show("保护半径必须为正数");
			this.hyTxtRadiu.Focus();
			return false;
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private List<ListItem> listInfos = new List<ListItem>();

		private UserSetting userSetting = new UserSetting();

		public FireHydrantParam fireHydrantParam;

		private string name = "";

		public int id;

		private Document doc;

		public FamilySymbol fs;

		private string familySymbol = "";

		private HyProjectFamily hyProjectFamily = new HyProjectFamily();

		public int LayType = 1;
	}
}
